<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="跟踪日期" prop="afterTime">
        <el-date-picker clearable
                        v-model="queryParams.afterTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择跟踪日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="跟踪目的" prop="afterGoal">
        <el-select v-model="queryParams.afterGoal" placeholder="请选择跟踪目的" clearable>
          <el-option
            v-for="dict in dict.type.sya_after_goal"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="学生状态" prop="stuType">
        <el-select v-model="queryParams.stuType" placeholder="请选择学生状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_student_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="跟踪次数" prop="afterCount">
        <el-input
          v-model="queryParams.afterCount"
          placeholder="请输入跟踪次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:after:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:after:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:after:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>


    <el-table v-loading="loading" :data="afterList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="学生跟踪ID" align="center" prop="id"/>
      <el-table-column label="所在班级" align="center" prop="className"/>
      <el-table-column label="学生" align="center" prop="stuName"/>
      <el-table-column label="跟踪日期" align="center" prop="afterTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.afterTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="跟踪目的" align="center" prop="afterGoal">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sya_after_goal" :value="scope.row.afterGoal"/>
        </template>
      </el-table-column>
      <el-table-column label="跟踪备注" align="center" prop="afterNote"/>
      <el-table-column label="跟踪人" align="center" prop="teacherName"/>
      <el-table-column label="学生状态" align="center" prop="stuType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_student_type" :value="scope.row.stuType"/>
        </template>
      </el-table-column>
      <el-table-column label="跟踪次数" align="center" prop="afterCount"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handUpdate(scope.row)"
            v-hasPermi="['system:after:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:after:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 修改 -->
    <el-dialog :title="title" :visible.sync="opens" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所在班级" prop="className">
          <el-select v-model="form.className" disabled placeholder="请选择所在班级">
            <el-option
              v-for="dict in classAll"
              :label="dict.name"
              :value="dict.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跟踪学生" prop="stuName">
          <el-select v-model="form.stuName" disabled placeholder="请选择">
            <el-option
              v-for="item in stuAll"
              :label="item.name"
              :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跟踪日期" prop="afterTime">
          <el-date-picker clearable
                          v-model="form.afterTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择跟踪日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="跟踪目的" prop="afterGoal">
          <el-select v-model="form.afterGoal" placeholder="请选择跟踪目的">
            <el-option
              v-for="dict in dict.type.sya_after_goal"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学生状态" prop="stuType">
          <el-select v-model="form.stuType" placeholder="请选择学生状态">
            <el-option
              v-for="dict in dict.type.sys_student_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跟踪备注" prop="afterNote">
          <el-input v-model="form.afterNote" placeholder="请输入跟踪备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加跟踪对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所在班级" prop="className">
          <el-select v-model="form.className" placeholder="请选择所在班级">
            <el-option
              v-for="dict in classAll"
              :label="dict.name"
              :value="dict.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跟踪学生" prop="stuName">
          <el-select v-model="form.stuName" multiple placeholder="请选择">
            <el-option
              v-for="item in stuAll"
              :label="item.name"
              :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跟踪日期" prop="afterTime">
          <el-date-picker clearable
                          v-model="form.afterTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择跟踪日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="跟踪目的" prop="afterGoal">
          <el-select v-model="form.afterGoal" placeholder="请选择跟踪目的">
            <el-option
              v-for="dict in dict.type.sya_after_goal"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学生状态" prop="stuType">
          <el-select v-model="form.stuType" placeholder="请选择学生状态">
            <el-option
              v-for="dict in dict.type.sys_student_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跟踪备注" prop="afterNote">
          <el-input v-model="form.afterNote" placeholder="请输入跟踪备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getStuAll,
  getClassAll,
  listAfter,
  getAfter,
  delAfter,
  addAfter,
  updateAfter
} from "@/api/help/after";

export default {
  name: "After",
  dicts: ['sya_after_goal', 'sys_student_type'],
  data() {
    return {
      stuAll: [],
      classAll: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生跟踪表格数据
      afterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      opens: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        className: null,
        stuName: null,
        afterTime: null,
        afterGoal: null,
        afterNote: null,
        teacherName: null,
        stuType: null,
        afterCount: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 获取班级下拉框
    getClassAll() {
      getClassAll().then(res => {
        this.classAll = res;
      })
    },

    getStuAll() {
      let para = Object.assign({}, this.form);
      getStuAll().then(res => {
        this.stuAll = res;
      })
    },

    /** 查询学生跟踪列表 */
    getList() {
      this.loading = true;
      listAfter(this.queryParams).then(response => {
        this.afterList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.opens = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        className: null,
        stuName: [],
        afterTime: null,
        afterGoal: null,
        afterNote: null,
        teacherName: null,
        stuType: null,
        afterCount: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生跟踪";
    },
    /** 修改按钮操作 */
    handUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAfter(id).then(response => {
        this.form = response.data;
        this.opens = true;
        this.title = "修改学生跟踪";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAfter(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.opens = false;
              this.getList();
            });
          } else {
            addAfter(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学生跟踪编号为"' + ids + '"的数据项？').then(function () {
        return delAfter(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/after/export', {
        ...this.queryParams
      }, `after_${new Date().getTime()}.xlsx`)
    }
  },
  mounted() {
    this.getClassAll();
    this.getStuAll();
  }
};
</script>
